//
//  FitmentViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/8/27.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "FitmentViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "FitmentCollectionViewCell.h"
#import "FitmentLayout.h"
#import "AFNetworking.h"
#import "Color.h"
#import "StyleModel.h"
#import "SpaceModel.h"
#import "FitmentModel.h"
#import "FitUser.h"
#import "FitUserImage.h"
#import "FitPhotoModel.h"
#import "FitPhotos.h"
#import "DecorateViewController.h"
#import "FitmentSonViewController.h"
#import "MJRefresh.h"
#import "JSDropDownMenu.h"
@interface FitmentViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, fitmentLayoutDelegate, UICollectionViewDelegateFlowLayout, JSDropDownMenuDelegate, JSDropDownMenuDataSource>
@property (nonatomic, strong)NSMutableArray <FitPhotos *> *imagesArr;
@property (nonatomic, strong)NSMutableArray <FitmentModel *> *fitmentColor;
@property (nonatomic, strong)FitmentModel *fitmentModel;
@property (nonatomic, strong)FitPhotoModel *fitPhotoModel;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, assign) CGFloat itemWidth;
@property (nonatomic, assign)UIWindow *window;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)NSNumber *color;
@property (nonatomic,strong)NSNumber *style;
@property (nonatomic, strong)NSNumber *space;
@property (nonatomic, strong)NSArray *titleArr;
@end

@implementation FitmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    image.image = [UIImage imageNamed:@"图库"];
    self.navigationItem.titleView = image;
    [self handleData];

    self.titleArr = @[@"空间", @"风格", @"色系"];
    _color = @0;
    _style = @0;
    _space = @21;
    // 上面按钮
    JSDropDownMenu *menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0, 0) andHeight:50];
    menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
    menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
    menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
    menu.dataSource = self;
    menu.delegate = self;
    [self.view addSubview:menu];
    
    [self creatCollectionView];
    // 下拉刷新, 上拉加载
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self handImages];
    }];
    [_collectionView.mj_header beginRefreshing];
    _collectionView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        [self creatMJRefreshFoot];
    }];
}
- (void)handleData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.guju.com.cn/v2/photo/map" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *colorDic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.fitmentModel = [FitmentModel modelWithDic:colorDic];
        [_collectionView reloadData];
        [_collectionView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
}
- (void)handImages {
    NSString *urlStr = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/photo/flow?sortId=2&start=%ld&count=10&userId=0&space=%@&style=%@&color=%@", (long)_page, _space, _style, _color ];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.fitPhotoModel = [FitPhotoModel modelWithDic:dic];
        _imagesArr = [NSMutableArray arrayWithArray:_fitPhotoModel.photos];
        [_collectionView reloadData];
        [_collectionView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
}
- (void)creatMJRefreshFoot {
    NSString *urlStr = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/photo/flow?sortId=2&start=%ld&count=10&userId=0&space=%@&style=%@&color=%@", (long)(_page += 10), _space, _style, _color];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.fitPhotoModel = [FitPhotoModel modelWithDic:dic];
        [_imagesArr addObjectsFromArray:_fitPhotoModel.photos];
        [_collectionView reloadData];
        [_collectionView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
}
// 创建collectionView
- (void)creatCollectionView {
    FitmentLayout *layout = [[FitmentLayout alloc] init];
    layout.delegate = self;
    layout.numberOfColumns = 2;
    layout.cellMargin = 5;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 35, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[FitmentCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
#pragma mark - collectionViewDatesoure
// item 的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _imagesArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FitmentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.photos = _imagesArr[indexPath.row];
    return cell;
}
#pragma mark - WaterFlowLayout Delegate
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
                    width:(CGFloat)width
 heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGRect boundingRect = CGRectMake(0, 0, width, CGFLOAT_MAX);
    CGSize size = CGSizeMake([_imagesArr[indexPath.row].width floatValue], [_imagesArr[indexPath.row].height floatValue]);
    CGRect newRect = AVMakeRectWithAspectRatioInsideRect(size, boundingRect);
    return newRect.size.height;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FitmentSonViewController *sonVC = [[FitmentSonViewController alloc] init];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:_fitPhotoModel.photos];
    sonVC.photoArr = arr;
    sonVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:sonVC animated:YES];
}
#pragma mark - JSDropDownMenu协议
- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    return 3;
}
- (BOOL)displayByCollectionViewInColumn:(NSInteger)column {
    return YES;
}
- (BOOL)haveRightTableViewInColumn:(NSInteger)column {
    return NO;
}
- (CGFloat)widthRatioOfLeftColumn:(NSInteger)column {
    return 1;
}
- (NSInteger)currentLeftSelectedRow:(NSInteger)column {
    
    return 0;
}
- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow {
    if (column == 0) {
        return _fitmentModel.space.count;
    } else if (column == 1) {
        return _fitmentModel.style.count;
    } else {
        return _fitmentModel.color.count;
    }

}
- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column {
    return _titleArr[column];
}
// 赋值
- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column==0) {
        return _fitmentModel.space[indexPath.row].name;
    } else if (indexPath.column == 1) {
        return _fitmentModel.style[indexPath.row].name;
    } else {
        return _fitmentModel.color[indexPath.row].name;
    }

}
// 接口
- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    if (indexPath.column==0) {
        _space = _fitmentModel.space[indexPath.row].space_id;
    } else if (indexPath.column == 1) {
        _style = _fitmentModel.style[indexPath.row].style_id;
        
    } else {
        _color = _fitmentModel.color[indexPath.row].color_id;
    }
    [_collectionView.mj_header beginRefreshing];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
